﻿using System.Collections.Generic;
using Baula.TextGen.Framework.Tools;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Baula.TextGen.Framework.Tests
{
    [TestClass]
    public class LineCollectorTests
    {
        [TestMethod]
        public void AppendNextLine_AddingLinesWithoutBlankLines_ReturnsThatLinesAsTextBlock()
        {
            const string expectedTextBlock =
                @"This is the textblock.
It does not contain any blank lines.
Just content spreading
multiple lines.";

            var gatherer = CreateLineGathererFilledWithLines(new[] {
                                                                       "This is the textblock.",
                                                                       "It does not contain any blank lines.",
                                                                       "Just content spreading",
                                                                       "multiple lines."
                                                                   });

            var actualTextBlock = gatherer.ToString();

            Assert.AreEqual(expectedTextBlock, actualTextBlock);
        }

        [TestMethod]
        public void AppendNextLine_AddingBlankLineAndNonBlankLine_ReturnsThatLinesWithTheBlankLine()
        {
            const string expectedTextBlock = "\r\nThis textblock started with a blank line.";

            var gatherer = CreateLineGathererFilledWithLines(new[] {
                                                                       "",
                                                                       "This textblock started with a blank line."
                                                                   });

            var actualTextBlock = gatherer.ToString();

            Assert.AreEqual(expectedTextBlock, actualTextBlock);
        }

        [TestMethod]
        public void AppendNextLine_AddingTwoBlankLinesAndNonBlankLine_ReturnsThatLinesWithTheBlankLines()
        {
            const string expectedTextBlock = "\r\n\r\nThis textblock started with two blank lines.";

            var gatherer = CreateLineGathererFilledWithLines(new[] {
                                                                       "",
                                                                       "",
                                                                       "This textblock started with two blank lines."
                                                                   });

            var actualTextBlock = gatherer.ToString();

            Assert.AreEqual(expectedTextBlock, actualTextBlock);
        }

        [TestMethod]
        public void AppendNextLine_AddingNonBlankLineAndBlankLine_ReturnsThatLinesWithTheBlankLine()
        {
            const string expectedTextBlock = "This textblock ends with a blank line.\r\n";

            var gatherer = CreateLineGathererFilledWithLines(new[] {
                                                                       "This textblock ends with a blank line.",
                                                                       ""
                                                                   });

            var actualTextBlock = gatherer.ToString();

            Assert.AreEqual(expectedTextBlock, actualTextBlock);
        }

        [TestMethod]
        public void AppendNextLine_AddingNonBlankLineAndTwoBlankLine_ReturnsThatLinesWithTheBlankLines()
        {
            const string expectedTextBlock = "This textblock ends with two blank lines.\r\n\r\n";

            var gatherer = CreateLineGathererFilledWithLines(new[] {
                                                                       "This textblock ends with two blank lines.",
                                                                       "",
                                                                       ""
                                                                   });

            var actualTextBlock = gatherer.ToString();

            Assert.AreEqual(expectedTextBlock, actualTextBlock);
        }

        [TestMethod]
        public void AppendNextLine_AddingLinesBlankNonBlankBlank_ReturnsThatLinesWithTheBlankLines()
        {
            const string expectedTextBlock = "\r\nThis textblock started and ends with a blank line.\r\n";

            var gatherer = CreateLineGathererFilledWithLines(new[] {
                                                                       "",
                                                                       "This textblock started and ends with a blank line.",
                                                                       ""
                                                                   });

            var actualTextBlock = gatherer.ToString();

            Assert.AreEqual(expectedTextBlock, actualTextBlock);
        }

        [TestMethod]
        public void AppendNextLine_AddingLinesWithWildBlankNonBlankMixture_ReturnsThatLinesPreservingAllTheBlankLines()
        {
            const string expectedTextBlock = @"

This is the first non blank line.

Another one.

And now a textblock spreading
multiple lines. That is because
of the unbelievable mass of text.

And yet another one.






And now the last one after several blank lines.
";

            var gatherer = CreateLineGathererFilledWithLines(
                new[] {
                          "",
                          "",
                          "This is the first non blank line.",
                          "",
                          "Another one.",
                          "",
                          "And now a textblock spreading",
                          "multiple lines. That is because",
                          "of the unbelievable mass of text.",
                          "",
                          "And yet another one.",
                          "",
                          "",
                          "",
                          "",
                          "",
                          "",
                          "And now the last one after several blank lines.",
                          ""
                      });

            var actualTextBlock = gatherer.ToString();

            Assert.AreEqual(expectedTextBlock, actualTextBlock);
        }

        private LineCollector CreateLineGathererFilledWithLines(IEnumerable<string> lines)
        {
            var gatherer = new LineCollector();

            foreach (var line in lines)
            {
                gatherer.AppendAsNewLine(line);
            }
            return gatherer;
        }
    }
}
